package com.xiaomi.baidumap.hotuser;

import android.util.Log;

import com.xiaomi.baidumap.common.LogUtils;
import com.xiaomi.baidumap.model.HotUserResult;
import com.xiaomi.baidumap.model.User;
import com.xiaomi.baidumap.network.GithubClient;


import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 16-8-31.
 */
public class HotUserPresenter {

    //利用接口来更新视图和加载数据
    public interface HotUserView{
        /*
        * 1.更新数据
        * 2.显示空白视图
        * 3.显示错误视图信息
        * 4.
        * */
        void showRefreshView();
        void refreshData(List<User> users);
        void stopRefresh();
        void showErrorView();
        void showEmptyView();
        void showMessage(String msg);

        //加载视图的方法
        void addLoadData(List<User> users);
        void showLoadView();
        void hideLoadView();

    }

    private Call<HotUserResult> userCall;
    private int nextPage = 1;
    private Call<HotUserResult> userResultCall;

    private HotUserView hotUserView;

    public HotUserPresenter(HotUserView hotUserView) {
        this.hotUserView = hotUserView;
    }

    //刷新的业务
    public void refresh(){
        hotUserView.showRefreshView();
        nextPage = 1;
        if (userCall != null){
            userCall.cancel();
        }
        userResultCall = GithubClient.getInstance().searchUsers("followers:"+">1000", nextPage);
        LogUtils.d("2222===");
        //异步处理
        userResultCall.enqueue(userCallback);
    }

    //加载的业务
    public void loadMore(){
        hotUserView.showLoadView();
        if (userCall != null){
            userCall.cancel();
        }
        userResultCall = GithubClient.getInstance().searchUsers("followers:"+">1000", nextPage);

        //异步处理
        userResultCall.enqueue(userLoadCallback);
    }

    private Callback<HotUserResult> userCallback = new Callback<HotUserResult>(){

        @Override
        public void onResponse(Call<HotUserResult> call, Response<HotUserResult> response) {
            LogUtils.d("ok");
            if (response.isSuccessful()){
                HotUserResult userResult = response.body();
                if (userResult == null){
                    //打印信息
                    hotUserView.showMessage("结果为空");
                    return;
                }
                //获得数据
                ArrayList<User> users = userResult.getUsers();
                LogUtils.d(users.toString() + "," + userResult.getTotalCount());
                //然后更新数据视图
                hotUserView.refreshData(users);
                hotUserView.stopRefresh();
                nextPage = 2;
                return;
            }
                //如果请求没有成功
                hotUserView.showMessage("响应码:"+response.code());
        }

        @Override
        public void onFailure(Call<HotUserResult> call, Throwable t) {
            hotUserView.stopRefresh();
            hotUserView.showMessage("刷新失败" + t.getMessage());
            LogUtils.d(t.getMessage());
        }
    };


    private Callback<HotUserResult> userLoadCallback = new Callback<HotUserResult>() {
        @Override
        public void onResponse(Call<HotUserResult> call, Response<HotUserResult> response) {
            hotUserView.hideLoadView();
            if (response.isSuccessful()){
                HotUserResult loadResult = response.body();
                if (loadResult == null){
                    hotUserView.showMessage("加载失败");
                    return;
                }

                ArrayList<User> users = loadResult.getUsers();
                //添加到加载视图上面
                hotUserView.addLoadData(users);
                nextPage++;
                return;
            }
            //如果请求没有成功
            hotUserView.showMessage("响应码"+response.code());
        }

        @Override
        public void onFailure(Call<HotUserResult> call, Throwable t) {
                hotUserView.showMessage("加载失败"+t.getMessage());
        }
    };

}
